package net.adetech.crj.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.dao.ApplyMapper;
import net.adetech.crj.dao.AppointmentMapper;
import net.adetech.crj.dao.RemindMapper;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.User;
import net.adetech.crj.entity.tb.*;
import net.adetech.crj.utils.ApiUtils;
import net.adetech.crj.utils.InnerServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service("outerApply.submit")
public class OuterApplySubmitService implements BaseService {

    @Autowired
    ApiUtils apiUtils;
    @Autowired
    ApplyMapper applyMapper;

    @Autowired
    AppointmentMapper appointmentMapper;

    @Autowired
    RemindMapper remindMapper;


    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ZggmtbForm form = (ZggmtbForm) params.get("form");
        User user = (User) params.get("user");
        if (user == null) {
            user = new User();
            user.setIdNumber("null");
        }

        ZggmtbResult result = new ZggmtbResult();
        List<Apply> applyList = new ArrayList<Apply>();
        JSONArray resultArray = new JSONArray();


        //办理港澳通行证
        if (form.getSqlb().contains("102")) {
            String ywbh = apiUtils.getYwbh();
            String args = getGaArgs(ywbh, form);

            JSONObject jsonObject = apiUtils.call("cn_saveApplyGazLabel", args);
            System.out.println(jsonObject.toJSONString());

            if ("0".equals(jsonObject.getString("success"))) {
                jsonObject.put("sqlb","102");
                resultArray.add(jsonObject);
            } else {
                Apply apply = new Apply();
                apply.setApplyUserId(user.getIdNumber());
                apply.setRealName(form.getZwx() + form.getZwm());
                apply.setIdNumber(form.getSfzh());
                apply.setIdType("1");
                apply.setApplySummary("OGA");
                apply.setApplyDetail(JSON.toJSONString(args));
                apply.setApplyNo(ywbh);
                apply.setPhoneNumber(form.getLxdh());
                apply.setRemindJson("[]");
                applyMapper.saveApply(apply);
                apply = applyMapper.getApplyByYwbh(ywbh);
                applyList.add(apply);
            }

        }

        //办理台湾通行证
        if (form.getSqlb().contains("104")) {
            String ywbh = apiUtils.getYwbh();
            String args = getTwArgs(ywbh, form);

            JSONObject jsonObject = apiUtils.call("cn_saveApplyFtzLabel", args);
            System.out.println(jsonObject.toJSONString());

            if ("0".equals(jsonObject.getString("success"))) {
                jsonObject.put("sqlb","104");
                resultArray.add(jsonObject);
            } else {

                Apply apply = new Apply();
                apply.setApplyUserId(user.getIdNumber());
                apply.setRealName(form.getZwx() + form.getZwm());
                apply.setIdNumber(form.getSfzh());
                apply.setIdType("1");
                apply.setApplySummary("OTW");
                apply.setApplyDetail(JSON.toJSONString(args));
                apply.setApplyNo(ywbh);
                apply.setPhoneNumber(form.getLxdh());
                apply.setRemindJson("[]");

                applyMapper.saveApply(apply);
                apply = applyMapper.getApplyByYwbh(ywbh);
                applyList.add(apply);
            }
        }


        return InnerServiceUtil.buildResult("applyList", applyList,"errorList",resultArray);
    }


    private String getGaArgs(String ywbh, ZggmtbForm form) {

        String sfzh = form.getSfzh();
        String xb = "1";
        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            xb = "2";
        }
        String csrq = sfzh.substring(6, 14);
        String sqrq = new SimpleDateFormat("yyyyMMdd").format(new Date());

        OuterApplyArgs args = new OuterApplyArgs();
        OuterApplyArgs.OuterApplyDto info = args.getInfo();
        info.setYwbh(ywbh);
        info.setSfzh(form.getSfzh());
        info.setZwx(form.getZwx());
        info.setZwm(form.getZwm());
        info.setCsrq(csrq);
        info.setXb(xb);
        info.setSqrq(sqrq);
        info.setHkszd(form.getHkszd());
        info.setXczjyxqz(form.getXczjyxqzGa());
        info.setXczjhm(form.getXczjhmGa());
        info.setXczjzl("21");
        info.setEmsdz(form.getEmsdz());
        info.setSjr(form.getSjr());
        info.setYzbm(form.getYzbm());
        info.setSjhm(form.getSjhm());
        info.setBzlb("92"); //办证类别  单独签注
        info.setSqlb("102");

        if (form.getQwd().contains("HKG")) {
            String qzcs = form.getQzcsHkg();
            String qzyxq = null;
            String qzyxqdw = null;
            String qzyxcs = qzcs.substring(3, 4);
            if (qzcs.substring(0, 2).equals("03")) {
                qzyxq = "3";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("06")) {
                qzyxq = "06";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("12")) {
                qzyxq = "1";
                qzyxqdw = "年";
            } else if (qzcs.substring(0, 2).equals("99")) {
                qzyxq = "3";
                qzyxqdw = "年";
            }

            OuterApplyArgs.Qzblxx qzblxx = new OuterApplyArgs().new Qzblxx();
            qzblxx.setQwd("HKG");
            qzblxx.setQzzl(form.getQzzlHkg());
            qzblxx.setQzyxcs(qzyxcs);
            qzblxx.setQzyxq(qzyxq);
            qzblxx.setQzyxqdw(qzyxqdw);

            info.getQzblxxs().add(qzblxx);


        }

        if (form.getQwd().contains("MAC")) {
            String qzcs = form.getQzcsMac();
            String qzyxq = null;
            String qzyxqdw = null;
            String qzyxcs = qzcs.substring(3, 4);
            if (qzcs.substring(0, 2).equals("03")) {
                qzyxq = "3";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("06")) {
                qzyxq = "6";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("12")) {
                qzyxq = "1";
                qzyxqdw = "年";
            } else if (qzcs.substring(0, 2).equals("99")) {
                qzyxq = "3";
                qzyxqdw = "年";
            }

            OuterApplyArgs.Qzblxx qzblxx = new OuterApplyArgs().new Qzblxx();
            qzblxx.setQwd("MAC");
            qzblxx.setQzzl(form.getQzzlHkg());
            qzblxx.setQzyxcs(qzyxcs);
            qzblxx.setQzyxq(qzyxq);
            qzblxx.setQzyxqdw(qzyxqdw);
            info.getQzblxxs().add(qzblxx);


        }

        return JSON.toJSONString(args);
    }

    private String getTwArgs(String ywbh, ZggmtbForm form) {


        String sfzh = form.getSfzh();
        String xb = "1";
        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            xb = "2";
        }
        String csrq = sfzh.substring(6, 14);
        String sqrq = new SimpleDateFormat("yyyyMMdd").format(new Date());

        OuterApplyArgs args = new OuterApplyArgs();
        OuterApplyArgs.OuterApplyDto info = args.getInfo();
        info.setYwbh(ywbh);
        info.setSfzh(form.getSfzh());
        info.setZwx(form.getZwx());
        info.setZwm(form.getZwm());
        info.setCsrq(csrq);
        info.setXb(xb);
        info.setSqrq(sqrq);
        info.setHkszd(form.getHkszd());
        info.setXczjyxqz(form.getXczjyxqzTw());
        info.setXczjhm(form.getXczjhmTw());
        info.setXczjzl("25");
        info.setEmsdz(form.getEmsdz());
        info.setSjr(form.getSjr());
        info.setYzbm(form.getYzbm());
        info.setSjhm(form.getSjhm());
        info.setBzlb("91"); //办证类别  单独签注
        info.setSqlb("104");


        String qzcs = form.getQzcsTwn();
        String qzyxq = null;
        String qzyxqdw = null;
        String qzyxcs = qzcs.substring(3, 4);
        if (qzcs.substring(0, 2).equals("03")) {
            qzyxq = "3";
            qzyxqdw = "月";
        } else if (qzcs.substring(0, 2).equals("06")) {
            qzyxq = "6";
            qzyxqdw = "月";
        } else if (qzcs.substring(0, 2).equals("12")) {
            qzyxq = "1";
            qzyxqdw = "年";
        } else if (qzcs.substring(0, 2).equals("99")) {
            qzyxq = "3";
            qzyxqdw = "年";
        }

        OuterApplyArgs.Qzblxx qzblxx = new OuterApplyArgs().new Qzblxx();
        qzblxx.setQwd("TWN");
        qzblxx.setQzzl(form.getQzzlTwn());
        qzblxx.setQzyxcs(qzyxcs);
        qzblxx.setQzyxq(qzyxq);
        qzblxx.setQzyxqdw(qzyxqdw);
        info.getQzblxxs().add(qzblxx);


        return JSON.toJSONString(args);
    }


}
